using CodePlex.TfsLibrary.ClientEngine;
using CodePlex.TfsLibrary.ObjectModel;

namespace CodePlex.CodePlexClientLibrary
{
    public class CommitViewPresenter : StatusViewPresenter
    {
        readonly ITfsEngine engine;
        readonly ICommitView view;

        public CommitViewPresenter(ICommitView view,
                                   ITfsEngine engine)
            : base(view, engine, false, true, false)
        {
            this.view = view;
            this.engine = engine;
        }

        public CommitViewPresenter(ICommitView view,
                                   ITfsEngine engine,
                                   string diffTool,
                                   string diffArgs,
                                   string mergeTool,
                                   string mergeArgs)
            : base(view, engine, false, true, false, diffTool, diffArgs, mergeTool, mergeArgs)
        {
            this.view = view;
            this.engine = engine;
        }

        public override bool Run(string localPath)
        {
            TfsFolderInfo folderInfo = engine.GetFolderInfo(localPath);

            if (folderInfo != null)
            {
                view.TfsServerUrl = folderInfo.TfsUrl;
                view.TfsServerPath = folderInfo.ServerPath;
            }

            bool result = base.Run(localPath);

            if (result)
                view.ShowComplete(engine.Commit(localPath, view.Message, view.ShowItemStatus));

            return result;
        }

        protected override bool ShouldShowItem(SourceItem item)
        {
            switch (item.LocalItemStatus)
            {
                case SourceItemStatus.Add:
                case SourceItemStatus.Conflict:
                case SourceItemStatus.Delete:
                case SourceItemStatus.Modified:
                    return true;
            }

            return false;
        }
    }
}